package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NCementDataWeightBo;
import com.ruoyi.zhgd.domain.vo.NCementDataWeightVo;
import com.ruoyi.zhgd.domain.NCementDataWeight;
import com.ruoyi.zhgd.mapper.NCementDataWeightMapper;
import com.ruoyi.zhgd.service.INCementDataWeightService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import com.baomidou.dynamic.datasource.annotation.DS;

/**
 * 水稳拌合站重量数据Service业务层处理
 *
 * @author zyb
 * @date 2023-09-06
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NCementDataWeightServiceImpl implements INCementDataWeightService {

    private final NCementDataWeightMapper baseMapper;

    /**
     * 查询水稳拌合站重量数据
     */
    @Override
    public NCementDataWeightVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询水稳拌合站重量数据列表
     */
    @Override
    public TableDataInfo<NCementDataWeightVo> queryPageList(NCementDataWeightBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NCementDataWeight> lqw = buildQueryWrapper(bo);
        Page<NCementDataWeightVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询水稳拌合站重量数据列表
     */
    @Override
    public List<NCementDataWeightVo> queryList(NCementDataWeightBo bo) {
        LambdaQueryWrapper<NCementDataWeight> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NCementDataWeight> buildQueryWrapper(NCementDataWeightBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NCementDataWeight> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getGravel1Act() != null, NCementDataWeight::getGravel1Act, bo.getGravel1Act());
        lqw.eq(bo.getGravel2Act() != null, NCementDataWeight::getGravel2Act, bo.getGravel2Act());
        lqw.eq(bo.getGravel3Act() != null, NCementDataWeight::getGravel3Act, bo.getGravel3Act());
        lqw.eq(bo.getGravel4Act() != null, NCementDataWeight::getGravel4Act, bo.getGravel4Act());
        lqw.eq(bo.getGravel5Act() != null, NCementDataWeight::getGravel5Act, bo.getGravel5Act());
        lqw.eq(bo.getGravel6Act() != null, NCementDataWeight::getGravel6Act, bo.getGravel6Act());
        lqw.eq(bo.getPowder1Act() != null, NCementDataWeight::getPowder1Act, bo.getPowder1Act());
        lqw.eq(bo.getPowder2Act() != null, NCementDataWeight::getPowder2Act, bo.getPowder2Act());
        lqw.eq(bo.getWaterAct() != null, NCementDataWeight::getWaterAct, bo.getWaterAct());
        lqw.eq(bo.getGravelAct() != null, NCementDataWeight::getGravelAct, bo.getGravelAct());
        lqw.eq(bo.getPowderAct() != null, NCementDataWeight::getPowderAct, bo.getPowderAct());
        lqw.eq(bo.getGravel1Theo() != null, NCementDataWeight::getGravel1Theo, bo.getGravel1Theo());
        lqw.eq(bo.getGravel2Theo() != null, NCementDataWeight::getGravel2Theo, bo.getGravel2Theo());
        lqw.eq(bo.getGravel3Theo() != null, NCementDataWeight::getGravel3Theo, bo.getGravel3Theo());
        lqw.eq(bo.getGravel4Theo() != null, NCementDataWeight::getGravel4Theo, bo.getGravel4Theo());
        lqw.eq(bo.getGravel5Theo() != null, NCementDataWeight::getGravel5Theo, bo.getGravel5Theo());
        lqw.eq(bo.getGravel6Theo() != null, NCementDataWeight::getGravel6Theo, bo.getGravel6Theo());
        lqw.eq(bo.getPowder1Theo() != null, NCementDataWeight::getPowder1Theo, bo.getPowder1Theo());
        lqw.eq(bo.getPowder2Theo() != null, NCementDataWeight::getPowder2Theo, bo.getPowder2Theo());
        lqw.eq(bo.getWaterTheo() != null, NCementDataWeight::getWaterTheo, bo.getWaterTheo());
        lqw.eq(bo.getPowderTheo() != null, NCementDataWeight::getPowderTheo, bo.getPowderTheo());
        lqw.eq(bo.getGravelTheo() != null, NCementDataWeight::getGravelTheo, bo.getGravelTheo());
        return lqw;
    }

    /**
     * 新增水稳拌合站重量数据
     */
    @Override
    public Boolean insertByBo(NCementDataWeightBo bo) {
        NCementDataWeight add = BeanUtil.toBean(bo, NCementDataWeight.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改水稳拌合站重量数据
     */
    @Override
    public Boolean updateByBo(NCementDataWeightBo bo) {
        NCementDataWeight update = BeanUtil.toBean(bo, NCementDataWeight.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NCementDataWeight entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除水稳拌合站重量数据
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
